package game.main;

import game.goods.Heart;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import engine.input.Key;

import java.util.ArrayList;

import static engine.app.View.keyH;
import static game.basic.GameBase.*;
import static game.main.GameApp.gameMusic;
import static game.main.GameApp.player;
import static game.main.GamePanel.gameState;

public class UI {
    //ui画笔,绘制ui界面
    private final GraphicsContext g2;

    // 菜单提示命令编号  >
    private int commandNum = 0;

    //显示在对话窗口的文字
    public String currentDialogue = "空空空,还不传?";

    //字体设置
    Font font_45;
    Font font_24;

    Heart heart;

    ArrayList<String> message = new ArrayList<>();
    ArrayList<Integer> messageCounter = new ArrayList<>();

    //背包的选择器
    private int slotCol = 0;
    private int slotRow = 0;

    public UI(GraphicsContext g2) {
        this.g2 = g2;
        g2.setTextAlign(TextAlignment.CENTER);
        font_45 = new Font(45);
        font_24 = new Font(24);
        heart = new Heart();
    }

    public void addMessage(String text) {
        message.add(text);
        messageCounter.add(0);
    }

    public void drawTitleScreen() {
        g2.setFont(font_45);
        g2.setFill(cWhite);
        int x = screenWidth / 2;
        int y = tileSize * 3;
        String text = "蓝色男孩大冒险";
        g2.fillText(text, x, y);

        g2.setFont(font_24);

        text = "新游戏";
        y += tileSize * 5;
        g2.fillText(text, x, y);

        text = "加载游戏";
        y += tileSize;
        g2.fillText(text, x, y);

        text = "退出";
        y += tileSize;
        g2.fillText(text, x, y);

        switch (commandNum) {
            case 0 -> g2.fillText(">  ", x - tileSize - 10, y - tileSize * 2);
            case 1 -> g2.fillText(">  ", x - tileSize - 10, y - tileSize);
            case 2 -> g2.fillText(">  ", x - tileSize - 10, y);
        }

        x = (screenWidth / 2) - tileSize;
        g2.drawImage(player.spriteMap.get(downDir)[0], x, tileSize * 4, tileSize * 2, tileSize * 2);

        titleGameState();
    }

    public void drawPauseScreen() {
        g2.setFont(font_45);
        g2.setFill(cWhite);
        g2.setTextAlign(TextAlignment.CENTER);
        int x = screenWidth / 2;
        int y = screenHeight / 2;
        g2.fillText("游戏暂停", x, y);
        g2.setFont(font_24);
        if (keyH.isPressed(Key.P)) {
            gameState = playState;
            gameMusic.playLoopMusic(0);
        }
    }

    public void drawDialogScreen() {
        int x = tileSize * 2;
        int y = tileSize / 2;
        int width = screenWidth - tileSize * 4;
        int height = tileSize * 4;

        drawSubWindow(x, y, width, height);

        x += tileSize * 6;
        y += tileSize;
        g2.setTextAlign(TextAlignment.CENTER);
        g2.setFill(cWhite);
        g2.fillText(currentDialogue, x, y);
    }

    public void drawCharacterScreen() {
        int frameX = tileSize;
        int frameY = tileSize;
        int frameWidth = tileSize * 5;
        int frameHeight = tileSize * 10;
        drawSubWindow(frameX, frameY, frameWidth, frameHeight);

        int textX = frameX + 25;
        int textY = frameY + tileSize;
        int lineHeight = 31;

        g2.setFill(cWhite);
        g2.setTextAlign(TextAlignment.LEFT);

        g2.fillText("等级:", textX, textY);
        textY += lineHeight;
        g2.fillText("生命值:", textX, textY);
        textY += lineHeight;
        g2.fillText("法力值:", textX, textY);
        textY += lineHeight;
        g2.fillText("力量:", textX, textY);
        textY += lineHeight;
        g2.fillText("敏捷:", textX, textY);
        textY += lineHeight;
        g2.fillText("攻击:", textX, textY);
        textY += lineHeight;
        g2.fillText("防御:", textX, textY);
        textY += lineHeight;
        g2.fillText("经验值:", textX, textY);
        textY += lineHeight;
        g2.fillText("下一级:", textX, textY);
        textY += lineHeight;
        g2.fillText("硬币:", textX, textY);
        textY += lineHeight + 20;
        g2.fillText("武器:", textX, textY);
        textY += lineHeight + 20;
        g2.fillText("防具:", textX, textY);

        textX = frameX + frameWidth - 85;
        textY = frameY + tileSize;
        String value;

        value = player.level + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.hp + "/" + player.maxHp;
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.mana + "/" + player.maxMana;
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.strength + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.dexterity + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.attack + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.defense + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.exp + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.nextLevelExp + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight;
        value = player.coin + " ";
        g2.fillText(value, textX, textY);

        textY += lineHeight - 12;
        textX -= 65;
        g2.drawImage(player.currentWeapon.objImg, textX, textY);

        textY += 52;
        g2.drawImage(player.currentShield.objImg, textX, textY);

    }

    public void drawMessage() {
        int msgX = tileSize * 3;
        int msgY = tileSize * 4;
        for (int i = 0; i < message.size(); i++) {
            if (message.get(i) != null) {
                g2.setFill(cBlack);
                g2.fillText(message.get(i), msgX + 2, msgY + 2);
                g2.setFill(cWhite);
                g2.fillText(message.get(i), msgX, msgY);

                int counter = messageCounter.get(i) + 1;
                messageCounter.set(i, counter);
                msgY += 60;
                if (messageCounter.get(i) > 180) {
                    message.remove(i);
                    messageCounter.remove(i);
                    return;
                }
            }
        }
    }

    //ui标题界面的按键操作
    public void titleGameState() {
        if (keyH.isPressed(Key.Enter) || keyH.isPressed(Key.UP) || keyH.isPressed(Key.DOWN)) {
            switch (keyH.isKey) {
                case UP -> {
                    commandNum--;
                    if (commandNum < 0) {
                        commandNum = 2;
                    }
                }
                case DOWN -> {
                    commandNum++;
                    if (commandNum > 2) {
                        commandNum = 0;
                    }
                }
                case Enter, Z -> {
                    if (commandNum == 0) {
                        gameState = playState;
                        gameMusic.playLoopMusic(0);
                    }
                    if (commandNum == 2) {
                        System.exit(0);
                    }
                }
            }
        }
    }

    //绘制生命值
    public void drawPlayerHp() {
        int x = tileSize / 2;
        int y = tileSize / 2;
        int i = 0;

        // 循环绘制玩家的生命值
        while (i < player.maxHp / 2) {
            g2.drawImage(heart.heart_blank, x, y);
            i++;
            x += tileSize;
        }
        //重置
        x = tileSize / 2;
        i = 0;
        while (i < player.hp) {
            g2.drawImage(heart.heart_half, x, y);
            i++;
            if (i < player.hp) {
                g2.drawImage(heart.objImg, x, y);
            }
            i++;
            x += tileSize;
        }
    }

    public void drawInventoryBag() {

        int frameX = tileSize * 9;
        int frameY = tileSize;
        int frameWidth = tileSize * 6 + 15;
        int frameHeight = tileSize * 5 + 10;
        drawSubWindow(frameX, frameY, frameWidth, frameHeight);//绘制背包窗口

        //更改背包元素的初始位置,将他放置在背包窗口内部
        int slotXStart = frameX + 20;
        int slotYStart = frameY + 20;
        int slotX = slotXStart;
        int slotY = slotYStart;

        //设置每个格子之间的间隙
        int slotSize = tileSize + 5;

        for (int i = 0; i < player.inventoryBag.size(); i++) {
            if (player.inventoryBag.get(i) == player.currentWeapon || player.inventoryBag.get(i) == player.currentShield) {
                g2.setFill(cGreen);
                g2.fillRoundRect(slotX, slotY, tileSize, tileSize, 10, 10);
            }

            g2.drawImage(player.inventoryBag.get(i).objImg, slotX, slotY);

            slotX += slotSize;
            //将背包的元素都绘制出来,而不是都在第一格
            switch (i) {
                case 4, 9, 14 -> {
                    slotX = slotXStart;
                    slotY += slotSize;
                }
            }
        }

        //绘制选择器
        int cursorX = slotXStart + (slotSize * slotCol);
        int cursorY = slotYStart + (slotSize * slotRow);
        g2.setLineWidth(2);
        g2.strokeRoundRect(cursorX, cursorY, tileSize, tileSize, 10, 10);

        int dFrameY = frameY + frameHeight + 10;
        int dFrameHeight = tileSize * 4;

        int bagIndex = getBagIndexOnSlot();

        int textX = frameX + 20;
        int textY = dFrameY + tileSize;

        if (bagIndex < player.inventoryBag.size()) {
            drawSubWindow(frameX, dFrameY, frameWidth, dFrameHeight);
            g2.setFill(cWhite);
            g2.setTextAlign(TextAlignment.LEFT);
            g2.fillText(player.inventoryBag.get(bagIndex).description, textX, textY);
            g2.setTextAlign(TextAlignment.CENTER);
        }

        openBagState();
    }


    // 绘制子窗口
    private void drawSubWindow(int x, int y, int width, int height) {
        g2.setFill(cBlack);
        g2.fillRoundRect(x, y, width, height, 35, 35);

        g2.setStroke(cWhite);
        g2.setLineWidth(5);
        g2.strokeRoundRect(x + 5, y + 5, width - 10, height - 10, 25, 25);
    }

    //背包窗口的操作
    private void openBagState() {
        if (keyH.isPressed(Key.UP) || keyH.isPressed(Key.DOWN) || keyH.isPressed(Key.LEFT) || keyH.isPressed(Key.RIGHT) || keyH.isPressed(Key.Enter) || keyH.isPressed(Key.Z) || keyH.isPressed(Key.C)) {
            switch (keyH.isKey) {
                case UP -> {
                    if (slotRow != 0) {
                        slotRow--;
                        gameMusic.playSE(9);
                    }
                }
                case LEFT -> {
                    if (slotCol != 0) {
                        slotCol--;
                        gameMusic.playSE(9);
                    }
                }
                case DOWN -> {
                    if (slotRow != 3) {
                        slotRow++;
                        gameMusic.playSE(9);
                    }
                }
                case RIGHT -> {
                    if (slotCol != 4) {
                        slotCol++;
                        gameMusic.playSE(9);
                    }
                }
                case Enter, Z -> player.selectItem();
                case C -> gameState = playState;
            }
        }
    }

    //获取背包位置索引
    public int getBagIndexOnSlot() {
        return slotCol + slotRow * 5;
    }


}
